const log4js = require('koa-log4');
const logger = log4js.getLogger('routes::site');
const router = require('koa-router')();
exports = module.exports = router;
const mongodb = require('mongodb');
const ObjectId = mongodb.ObjectId;

const util = require(global.prefixPath + '/commons/util');
const info_service = require(global.prefixPath + '/services/info');
const user_service = require(global.prefixPath + '/services/user');


router.get('/infos', async (ctx, next) => {

    try {
        let ids = ctx.query.ids;
        if (ids) {
            ids = ids.replace(/^\s+|\s+$/g,'')
            ids = ids.replace(/\"/g, "");
            ids = ids.replace(/\'/g, "");
            ids = ids.split(',');
            for (let i = 0; i < ids.length; i++) {
                let id = ids[i];
                ids[i] = id.replace(/\"/g, "").replace(/\'/g, "").replace(/^\s+/, '').replace(/\s+$/, '');
            }
            ids = util.toObjectId(ids);
            let result = await info_service.findByIds(ids);
            ctx.body = result;
            return
        }

        let q = ctx.query.q;
        if (q) {
            q = JSON.parse(q);
            let result = await info_service.find(q);
            ctx.body = result;
            return
        }

    } catch (err) {
        throw err;
    }
});

router.get('/infos/:hash', async (ctx, next) => {

    let hash = ctx.params.hash;
    let x_query = ctx.req.headers["x-query"];
    x_query = new Buffer(x_query, 'base64').toString();
    let q = JSON.parse(x_query);

    try {
        let result = await info_service.find(q);
        ctx.body = result;
    } catch (err) {
        throw err;
    }

});
router.get('/infos/:query/search', async (ctx, next) => {

    let query = ctx.params.query;
    console.log('query = ')
    console.log(query);
    let q = JSON.parse(query);
    try {
        let result = await info_service.find(q);
        ctx.body = result;
    } catch (err) {
        throw err;
    }

});
// router.get('/infos/:query/collect', async (ctx, next) => {
//     try {
//         let q = ctx.params.id;
//         let result = await info_service.find(info_id);
//         ctx.body = result;
//     } catch (err) {
//         throw err;
//     }
// });
/**
 *
 */
router.get('/info/:id', async (ctx, next) => {
    try {
        let info_id = ctx.params.id;
        let result = await info_service.findById(info_id);
        ctx.body = result;
    } catch (err) {
        throw err;
    }
});

/**
 * 添加信息
 */
router.post('/info', async (ctx, next) => {
    try {
        let info = ctx.request.body;
        let result = await info_service.insert(info);
        ctx.body = result;
    } catch (err) {
        throw err;
    }
});

/**
 * 修改信息
 */
router.put('/info/:id', async (ctx, next) => {
    try {
        let info_id = ctx.params.id;
        let info = ctx.request.body;
        info._id = new ObjectId(info_id);
        let result = await info_service.save(info);
        ctx.body = result;
    } catch (err) {
        throw err;
    }
});

/**
 * 查看信息(viewCount+1)
 */
router.put('/info/:id/view', async (ctx, next) => {
    try {
        let info_id = ctx.params.id;
        let oid = new ObjectId(info_id);
        let update = {
            query: {_id: oid},
            update: {$inc: {viewCount: 1}}
        };
        let result = await info_service.findAndModify(update.query, update.update);
        ctx.body = {
            result: result
        };
    } catch (err) {
        throw err;
    }
});
/**
 * 信息成交
 */
router.put('/info/:id/offline', async (ctx, next) => {
    try {
        let site_id = ctx.params.id;
        let oid = new ObjectId(site_id);
        let update = {
            query: {_id: oid},
            update: {$set: {isTraded: true,tradedAt: new Date()}}
        };
        let result = await info_service.findAndModify(update.query, update.update);
        ctx.body = {
            result: result
        };
    } catch (err) {
        throw err;
    }
});
/**
 * 信息成交
 */
router.put('/info/:id/online', async (ctx, next) => {
    try {
        let site_id = ctx.params.id;
        let oid = new ObjectId(site_id);
        let update = {
            query: {_id: oid},
            update: {$set: {isTraded: false}}
        };
        let result = await info_service.findAndModify(update.query, update.update);
        ctx.body = {
            result: result
        };
    } catch (err) {
        throw err;
    }
});
/**
 * 关注信息
 */
router.put('/info/:id/watch', async (ctx, next) => {
    try {
        let info_id = ctx.params.id;
        let oid = new ObjectId(info_id);
        let update = {
            query: {_id: oid},
            update: {$inc: {watchCount: 1}}
        };
        let result = await info_service.findAndModify(update.query, update.update);
        ctx.body = result;
    } catch (err) {
        throw err;
    }
});

/**
 * 信息已成交
 */
router.put('/info/:id/done', async (ctx, next) => {
    try {
        let info_id = ctx.params.id;
        let oid = new ObjectId(info_id);
        let update = {
            query: {_id: oid},
            update: {$set: {isDone: true}}
        };
        let result = await info_service.findAndModify(update.query, update.update);
        ctx.body = result;
    } catch (err) {
        throw err;
    }
});

/**
 * 删除信息
 */
router.delete('/info/:id', async (ctx, next) => {
    try {
        let info_id = ctx.params.id;
        let result = await info_service.delById(info_id);
        ctx.body = result;
    } catch (err) {
        throw err;
    }
});

/**
 * 批量删除信息
 */
router.delete('/infos/:ids', async (ctx, next) => {
    try {
        let info_id = ctx.params.ids;
        console.log(info_id);
        let result = await info_service.delByIds(info_id);
        ctx.body = result;
    } catch (err) {
        throw err;
    }
});
